'Copyright (c) 2008, Jonathan Allen
Imports ClrExtensions
Imports WrapNetflix
Imports ClrExtensions.Net.Rest

Public MustInherit Class CatalogTitle
	Inherits NetflixResource

	Protected Sub New(ByVal connection As NetflixConnection, ByVal source As XElement)
		MyBase.New(connection, source)
	End Sub

	Public Overrides ReadOnly Property Id() As String
		Get
			Return Source.<id>.Value
		End Get
	End Property

	Public Overrides ReadOnly Property Title() As String
		Get
			Return Source.<title>.@regular
		End Get
    End Property

    Public ReadOnly Property TitleRef() As String
        Get
            If (Not FindRelLink("http://schemas.netflix.com/catalog/title") Is Nothing) Then
                Return FindRelLink("http://schemas.netflix.com/catalog/title").Id
            Else
                Return Id
            End If
        End Get
    End Property

    Public ReadOnly Property TitleRefNode() As ResourceLink
        Get
            Return FindRelLink("http://schemas.netflix.com/catalog/title")
        End Get
    End Property

	Public ReadOnly Property ShortTitle() As String
		Get
			Return Source.<title>.@short
		End Get
	End Property

    Public Property UserRating() As Rating
        Get
            Return Source.<user_rating>.Value.RatingEnum
        End Get
        Set(ByVal value As Rating)
            If (Source.<user_rating>.Value Is Nothing) Then
                Source.Add(New XElement("user_rating"))
            End If
            Source.<user_rating>.Value = value.RatingString
        End Set
    End Property

    Public ReadOnly Property BoxArt(ByVal size As BoxArtSize) As Uri
        Get
            Dim temp As String = Nothing
            Select Case size
                Case BoxArtSize.Large : temp = Source.<box_art>.@large
                Case BoxArtSize.Medium : temp = Source.<box_art>.@medium
                Case BoxArtSize.Small : temp = Source.<box_art>.@small
                Case Else : Throw New ArgumentOutOfRangeException("size")
            End Select
            If temp <> "" Then Return New Uri(temp) Else Return Nothing
        End Get
    End Property

	Public ReadOnly Property SynopsisLink() As ResourceLink
		Get
			Return FindLink("synopsis")
		End Get
	End Property

	Public ReadOnly Property TvRatings() As IList(Of String)
		Get
			Return FindCategories("http://api.netflix.com/categories/tv_ratings")
		End Get
	End Property

	Public ReadOnly Property MpaaRatings() As IList(Of String)
		Get
			Return FindCategories("http://api.netflix.com/categories/mpaa_ratings")
		End Get
	End Property

	Public ReadOnly Property Genres() As IList(Of String)
		Get
			Return FindCategories("http://api.netflix.com/categories/genres")
		End Get
	End Property

    Public ReadOnly Property ReleaseYear() As Integer?
        Get
            Return CInt2(Source.<release_year>.Value)
        End Get
    End Property

	Public ReadOnly Property Runtime() As TimeSpan?
		Get
			Dim minutes As Integer = CInt(Source.<runtime>.Value)
			If minutes > 0 Then Return New TimeSpan(0, 0, minutes)
		End Get
	End Property

	Public ReadOnly Property FormatsLink() As ResourceLink
		Get
			Return FindLink("formats")
		End Get
	End Property

	Public ReadOnly Property ScreenFormatsLink() As ResourceLink
		Get
			Return FindLink("screen formats")
		End Get
	End Property

	Public ReadOnly Property CastLink() As ResourceLink
		Get
			Return FindLink("cast")
		End Get
	End Property

	Public ReadOnly Property DirectorsLink() As ResourceLink
		Get
			Return FindLink("directors")
		End Get
	End Property

	Public ReadOnly Property LanguagesAndAudioLink() As ResourceLink
		Get
			Return FindLink("languages and audio")
		End Get
	End Property

	Public ReadOnly Property AverageRating() As Decimal
		Get
			Return CDec(Source.<average_rating>.Value)
		End Get
	End Property

	Public ReadOnly Property SeriesLink() As ResourceLink
		Get
			Return FindLink("series")
		End Get
	End Property

	Public ReadOnly Property DiscsLink() As ResourceLink
		Get
			Return FindLink("discs")
		End Get
	End Property

	Public ReadOnly Property EpisodesLink() As ResourceLink
		Get
			Return FindLink("episodes")
		End Get
	End Property

	Public ReadOnly Property SimilarsLink() As ResourceLink
		Get
			Return FindLink("similars")
		End Get
	End Property

	Public ReadOnly Property OfficialWebpageLink() As ResourceLink
		Get
			Return FindLink("official webpage")
		End Get
	End Property

	Public ReadOnly Property WebPageLink() As Uri
		Get
			Dim linkNode As String = FindLink("web page").Id
			Return If(linkNode <> "", New Uri(linkNode), Nothing)
		End Get
    End Property

	Public ReadOnly Property AwardsLink() As ResourceLink
		Get
			Return FindLink("awards")
		End Get
    End Property

    Public ReadOnly Property DeliveryFormats() As IList(Of String)
        Get
            Return FindCategories("http://api.netflix.com/categories/title_formats")
        End Get
    End Property

    Public ReadOnly Property LinkNames() As IList(Of String)
        Get
            Return (From node In Source.<link> Select node.@title).ToList
        End Get
    End Property


	'Public Function GetDetails(ByVal expansions As TitleExpansions) As CatalogTitle
	'	Return Connection.Catalog.GetTitleDetails(Me.Id, expansions)
	'End Function


	Public Shared Function ParseEntry(ByVal connection As NetflixConnection, ByVal source As XElement) As CatalogTitle
		'TODO - Replace the prefix matching with a real RegEx

		Dim id = source.<id>.Value
		If id.StartsWith(CatalogDisc.IdPrefix) Then	  'disc
			Return New CatalogDisc(connection, source)
		ElseIf id.StartsWith(CatalogSeries.IdPrefix) Then 'series
			If id.Contains("/seasons/") Then 'this is really a season
				Return New CatalogSeason(connection, source)
			Else
				Return New CatalogSeries(connection, source)
			End If
		ElseIf id.StartsWith(CatalogMovie.IdPrefix) Then 'movie
			Return New CatalogMovie(connection, source)
		Else
			Throw New FormatException("Cannot parse the id & " & id)
		End If
	End Function


    'Protected Function Discs() As CatalogTitleCollection
    '	Static m_Discs As CatalogTitleCollection

    '	If m_Discs Is Nothing Then
    '		Dim discsNode = FindLink("discs")

    '		If discsNode Is Nothing Then
    '			m_Discs = New CatalogTitleCollection(New List(Of CatalogTitle))
    '			Return m_Discs
    '		End If

    '		discsNode.Load()
    '		m_Discs = New CatalogTitleCollection(discsNode.GetCatalogTitles)
    '	End If

    '	Return m_Discs
    'End Function

End Class
